package com.coocaa.ops.api.v1.params.provider;

import com.coocaa.ops.api.base.third.dmp.IDmpService;
import com.coocaa.ops.api.common.model.HeaderModel;
import com.coocaa.ops.api.v1.params.cache.IOpsParamsCache;
import com.coocaa.ops.api.v1.params.handler.ParamsHandler;
import com.coocaa.ops.api.v1.params.model.OpsParamModel;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author zen
 * @date 2020-11-03 14:15
 * @description
 */
@Slf4j
@Service
public class ParamsProviderImpl implements IParamsProvider {


    @Autowired
    private IDmpService dmpService;


    @Autowired
    private IOpsParamsCache opsParamsCache;

    @Override
    public List<OpsParamModel> getOpsParamModelList(HeaderModel header) {
        List<Integer> opsParamsIds = opsParamsCache.getOpsParamsAllIdsCache();
        return getOpsParamsListFromCache(header, opsParamsIds);
    }

    @Override
    public List<OpsParamModel> getOpsParamModelList(HeaderModel header, String type) {
        List<Integer> opsParamsIds = opsParamsCache.getOpsParamsIdsByTypeCache(type);
        return getOpsParamsListFromCache(header, opsParamsIds);
    }


    private List<OpsParamModel> getOpsParamsListFromCache(HeaderModel header, List<Integer> opsParamsIds) {
        if (CollectionUtils.isNotEmpty(opsParamsIds)) {
            Set<Integer> policyIds = InstanceUtil.newHashSet();
            List<OpsParamModel> handleParamsList = InstanceUtil.newArrayList();
            for (Integer id : opsParamsIds) {
                List<OpsParamModel> paramsList = opsParamsCache.getOpsParamsCache(id);
                if (CollectionUtils.isNotEmpty(paramsList)) {
                    for (OpsParamModel paramModel : paramsList) {
                        if (ParamsHandler.isDmpParams(paramModel)) {
                            policyIds.add(paramModel.getDmpInfo().getPolicyId());
                        }
                    }
                    handleParamsList.addAll(paramsList);
                }
            }
            if (CollectionUtils.isEmpty(handleParamsList)) {
                log.info("参数下发业务：缓存getOpsParamsCache为空，返回null");
                return null;
            }
            return getResultParamsList(header, policyIds, handleParamsList);
        }
        log.info("参数下发业务：获取不到对应的参数id为空，返回null");
        return null;
    }

    private List<OpsParamModel> getResultParamsList(HeaderModel header, Set<Integer> policyIds, List<OpsParamModel> handleParamsList) {
        List<OpsParamModel> resultList = InstanceUtil.newArrayList(handleParamsList.size());
        for (OpsParamModel opsParamModel : handleParamsList) {
            if (!ParamsHandler.isDmpParams(opsParamModel)) {
                if (StringUtils.isNotBlank(opsParamModel.getParams())) {
                    resultList.add(opsParamModel);
                }
            }
        }
        if (CollectionUtils.isNotEmpty(policyIds)) {
            Set<Integer> crowdIds = dmpService.getMatchedCrowdIdsFromDmp(policyIds, header);
            if (CollectionUtils.isNotEmpty(crowdIds)) {
                for (Integer crowdId : crowdIds) {
                    for (OpsParamModel opsParamModel : handleParamsList) {
                        if (crowdId.equals(opsParamModel.getDmpInfo().getCrowdId())) {
                            resultList.add(opsParamModel);
                        }
                    }
                }
            } else {
                log.info("参数下发业务:" + "用户MAC[" + header.getMac() + "] 使用策略组:[" + policyIds.toString() + "] 未能命中对应的人群");
            }
        }
        return resultList;
    }
}
